﻿
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.MTHHelper;
using thinger.MTHModels;

namespace thinger.MTHDAL
{
    public class SysAdminServer
    {
        //查询sysAdmin
        public SysAdmin AdminLogin(SysAdmin sysAdmin)
        {
            string sql = "Select LoginId,LoginName,LoginPwd,ParamSet,Recipe, HistoryLog,HistoryTrend,UserManage";

            sql += "from sys_admin where LoginName=@LoginName and LoginPwd=@LoginPwd";

            //设置参数
           
            MySqlParameter[] mySqlParameters = new MySqlParameter[]
            {
                new MySqlParameter("@LoginName", sysAdmin.LoginName),
                new MySqlParameter("@LoginPwd", sysAdmin.LoginPwd)
            };

            DataSet dataSet = thinger.MTHHelper.MySqlHelper.GetDataSet(sql, mySqlParameters);

            if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count == 1)
            {
                sysAdmin.LoginId = (int)dataSet.Tables[0].Rows[0]["LoginId"];
                sysAdmin.LoginName = dataSet.Tables[0].Rows[0]["LoginName"].ToString();
                sysAdmin.LoginPwd = dataSet.Tables[0].Rows[0]["LoginPwd"].ToString();
                sysAdmin.ParamSet = dataSet.Tables[0].Rows[0]["ParamSet"].ToString().Trim() == "1" ? true : false;
                sysAdmin.Recipe = dataSet.Tables[0].Rows[0]["Recipe"].ToString().Trim() == "1" ? true : false;
                sysAdmin.HistoryLog = dataSet.Tables[0].Rows[0]["HistoryLog"].ToString().Trim() == "1" ? true : false;
                sysAdmin.HistoryTrend = dataSet.Tables[0].Rows[0]["HistoryTrend"].ToString().Trim() == "1" ? true : false;
                sysAdmin.UserManage = dataSet.Tables[0].Rows[0]["UserManage"].ToString().Trim() == "1" ? true : false;
                return sysAdmin;
            }

            return null;

        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public DataTable AdminLoginAll()
        {
            string sql = "Select LoginId,LoginName,LoginPwd,ParamSet,Recipe, HistoryLog,HistoryTrend,UserManage";

            sql += "from sys_admin";

            DataSet dataSet = thinger.MTHHelper.MySqlHelper.GetDataSet(sql);

            if (dataSet != null && dataSet.Tables.Count > 0)
            {
                return dataSet.Tables[0];
            }

            return null;

        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public int AddLogin(SysAdmin sysAdmin)
        {
            string sql = "Insert into sys_admin (LoginName,LoginPwd,ParamSet,Recipe,HistoryLog,HistoryTrend,UserManage)";

            sql += "values (@LoginName,@LoginPwd,@ParamSet,@Recipe,@HistoryLog,@HistoryTrend,@UserManage)";

            //设置参数
            MySqlParameter[] mySqlParameters = new MySqlParameter[]
            {
                new MySqlParameter("@LoginName",sysAdmin.LoginName),
                new MySqlParameter("@LoginPwd", sysAdmin.LoginPwd),
                new MySqlParameter("@ParamSet", sysAdmin.ParamSet),
                new MySqlParameter("@Recipe", sysAdmin.Recipe),
                new MySqlParameter("@HistoryLog", sysAdmin.HistoryLog),
                new MySqlParameter("@HistoryTrend", sysAdmin.HistoryTrend),
                new MySqlParameter("@UserManage", sysAdmin.UserManage)
            };

            return thinger.MTHHelper.MySqlHelper.ExecuteSql(sql,mySqlParameters);

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sysAdmin"></param>
        public int UpdateLogin(SysAdmin sysAdmin)
        {
            string sql = "Update sys_admin Set LoginName=@LoginName,LoginPwd=@LoginPwd,ParamSet=@ParamSet,Recipe=@Recipe,HistoryLog=@HistoryLog,HistoryTrend=@HistoryTrend,UserManage=@UserManage";

            sql += "Where LoginName = @UpLoginName";

            //设置参数
            MySqlParameter[] mySqlParameters = new MySqlParameter[]
            {
                new MySqlParameter("@LoginName",sysAdmin.LoginName),
                new MySqlParameter("@LoginPwd", sysAdmin.LoginPwd),
                new MySqlParameter("@ParamSet", sysAdmin.ParamSet),
                new MySqlParameter("@Recipe", sysAdmin.Recipe),
                new MySqlParameter("@HistoryLog", sysAdmin.HistoryLog),
                new MySqlParameter("@HistoryTrend", sysAdmin.HistoryTrend),
                new MySqlParameter("@UserManage", sysAdmin.UserManage),
                new MySqlParameter("@UpLoginName", sysAdmin.LoginName)
            };

            return MTHHelper.MySqlHelper.ExecuteSql(sql,mySqlParameters);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public int DeleteAdmin(SysAdmin sysAdmin)
        {
            string sql = "Delete from sys_admin";

            sql += "Where LoginName = @LoginName";

            //设置参数
            MySqlParameter[] mySqlParameters = new MySqlParameter[]
            {
                new MySqlParameter("LoginName",sysAdmin.LoginName)
            };
            return MTHHelper.MySqlHelper.ExecuteSql(sql, mySqlParameters);


        }

       
    }
}
